// @ts-ignore
import {TimeUtils} from '@localModules/ZhiXinLib/utils/TimeUtils'
// @ts-ignore
import typeOfUtils from "@localModules/ZhiXinLib/utils/type-of-utils";
// @ts-ignore
import listTreeUtils from './listTree2List'
// @ts-ignore
import beautify from 'js-beautify/js'
// @ts-ignore
import textUtils from "@localModules/ZhiXinLib/utils/textUtils";
// @ts-ignore
import getData from "@/utils/excelToTableHead";

export class TableCodeGenItem {
  public label: string = '表头文字'
  public prop?: string = '表字段'
  public children?: TableCodeGenItem[] = []
}

export default class tableCodeGenUtils {
  public static propCount: number = 0

  public static initPropCount() {
    tableCodeGenUtils.propCount = 0
  }

  /**
   * excel生成表头
   * @param file
   * @param sheetName
   * @param rowTop
   * @param rowEnd
   * @param colBegin
   * @param colEnd
   */
  public static async excelToTableHead(file: File,
                                       sheetName: string = '',
                                       rowTop: string = '1',
                                       rowEnd: string = '',
                                       colBegin: string = 'A',
                                       colEnd: string = ''): Promise<TableCodeGenItem[]> {
    const begin = colBegin + rowTop
    const finish = colEnd + rowEnd
    return await getData(file, sheetName, begin, finish)
  }

  /**
   * 表头代码生成easyExcel的javaBean
   * @param tableHead
   * @param genNumber
   */
  public static tableHeadToEasyJavaBean(tableHead: TableCodeGenItem[],genNumber:string): string {
    let result: string = 'package top.xzxsrq.genutils.excel.vo;\n' +
      '\n' +
      'import com.alibaba.excel.annotation.ExcelProperty;\n' +
      'import lombok.Data;\n' +
      '/**\n' +
      ' * @program: zhixin前端生成\n' +
      ' * @create: ' + TimeUtils.parseTime(new Date(), '{y}-{m}-{d} {h}:{i}') + '\n' +
      ' **/\n' +
      '@Data\n' +
      'public class ZhiXinExcelVo {\n'
    if (typeOfUtils.isEmpty(tableCodeGenUtils)) {
      return '表头为空'
    }
    const identifier: string = '_childrenIndex'
    const listTree2List = listTreeUtils.listTree2List(tableHead, false, 'children', identifier);
    const lastChildren = listTreeUtils.findLastChildren(listTree2List, identifier)
    console.log(lastChildren);
    lastChildren.forEach((i: TableCodeGenItem) => {
      const findParentByChildIndex = listTreeUtils.findParentByChildIndex(listTree2List, i, true, identifier);
      findParentByChildIndex.push(i)
      let label = ''
      findParentByChildIndex.forEach((pItem: TableCodeGenItem) => {
        label += (`"${pItem.label}",`)
      })
      label = label.substr(0, label.length - 1)
      let javaType = 'java.lang.String'
      const isBigDecimal = genNumber.split(',')
      if (isBigDecimal.find(i => label.includes(i))) {
        javaType = 'java.math.BigDecimal'
      }
      const item = '\n' +
        '    @ExcelProperty(value = {' + label + '})\n' +
        // @ts-ignore
        '    protected ' + javaType + ' ' + i.prop.replaceAll('\'', '') + ';\n'
      result += item
    })
    result += '\n' +
      '}\n'
    return result
  }
}
